﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using DataTier;
using DataAccessTier;
using DataAccessTier.MembershipTableAdapters;

namespace BusinessTier
{
    [DataObject(true)]
    public class Memberships
    {
        ///// <summary>
        ///// Select method which returns membership details
        ///// </summary>
        ///// <param name="studentNum">Passed through codebehind of current.user.name attribute</param>
        ///// <returns>Datatable containing member details</returns>
        //[DataObjectMethod(DataObjectMethodType.Select, true)]
        //public static Membership.MemberDataTable getMember(string studentNum)
        //{
        //    MemberTableAdapter MemberAdapter = new MemberTableAdapter();
        //    Membership.MemberDataTable memberData = MemberAdapter.GetData(studentNum);
        //    return memberData;
        //}

        public static void updateMember(string Member)
        {
            System.Diagnostics.Trace.WriteLine(Member, "Update");
        }

        public static void deleteMember(string Member)
        {
            System.Diagnostics.Trace.WriteLine(Member, "Delete");
        }

        public static void createGroup(string studentID)
        {
            select_groups1TableAdapter SelectGroups = new select_groups1TableAdapter();
            SelectGroups.Insert(studentID);
        }

        public static void withdrawGroup(string studentID)
        {
            select_groups1TableAdapter WithdrawGroups = new select_groups1TableAdapter();
            WithdrawGroups.Update(studentID);
        }

        //public static void joinGroup(string studentID, Int16 groupID)
        //{
        //    select_groupsTableAdapter SelectGroups = new select_groupsTableAdapter();
        //    SelectGroups.Update(studentID, groupID);
        //}

        [DataObjectMethod(DataObjectMethodType.Select, true)]
        public static Membership.select_groupsDataTable selectAllGroups(string userID)
        {
            select_groupsTableAdapter groupsAdapter = new select_groupsTableAdapter();
            Membership.select_groupsDataTable UserData = groupsAdapter.GetData(userID);
            return UserData;
        }



        /// <summary>
        /// Runs a simple check of the username and password entered 
        /// to the database. 
        /// </summary>
        /// <param name="Username">String taken from username textbox of login control</param>
        /// <param name="Password">String taken from password textbox of login control</param>
        /// <returns>Boolean specifying whether username and password can be found in database</returns>
        public static bool isUser(string Username, string Password)
        {
            select_studentsTableAdapter LogonAdapter = new select_studentsTableAdapter();
            Membership.select_studentsDataTable LogonDetails = LogonAdapter.GetData();

            bool logon = false;
            int isAuthenticated = 0;

            foreach (Membership.select_studentsRow LogonRow in LogonDetails)
            {
                if ((Username == LogonRow.user_id) && (Password == LogonRow.password))
                {
                    isAuthenticated++;
                }
            }

            if (isAuthenticated > 0)
            {
                logon = true;
            }
            else { logon = false; }

            return logon;
        }

        public static string getUserRoles(string username)
        {
            string memberRole = "";
            usersTableAdapter RolesAdapter = new usersTableAdapter();
            Membership.usersDataTable MemberData = RolesAdapter.GetData(username);
            if (MemberData[0].user_type == "student")
            {
                memberRole = "student";
            }
            if (MemberData[0].user_type == "student")
            {
                memberRole = "student";
            }
            return memberRole;
        }
    }
}
